namespace BM.Tools.WebControls.MForm.Navigation.Predicates
{
    using BM.Tools.WebControls.MForm.Navigation.XPath;
    using BM.Tools.WebControls.MForm.Navigation.XPath.Predicates;

    /// <summary>
    /// A navigator for the <see cref="AlternativesPredicate" /> predicate locator.
    /// </summary>
    /// <typeparam name="T">
    /// The type of the navigable object.
    /// </typeparam>
    public class AlternativesPredicateNavigator<T> : PredicateNavigator<T, AlternativesPredicate>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AlternativesPredicateNavigator{T}"/> class. 
        /// </summary>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <param name="locator">
        /// The locator.
        /// </param>
        public AlternativesPredicateNavigator(INavigator<T> parent, AlternativesPredicate locator)
                : base(parent, locator)
        {
        }

        /// <summary>
        /// for all <see cref="AlternativesPredicate" /> contents, creates a navigator and performs test with the predicate function.
        /// </summary>
        /// <param name="navigable">
        /// The navigable.
        /// </param>
        /// <returns>
        /// Returns true if any of the performed tests returned true, otherwise returns false.
        /// </returns>
        public override bool Matches(T navigable)
        {
            foreach (Predicate predicate in Locator.Contents)
            {
                var predicateNavigator = PredicateNavigatorFactory<T>.Create(this, predicate);
                if (predicateNavigator.PredicateFunction.Invoke(navigable))
                {
                    return true;
                }
            }

            return false;
        }
    }
}